<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;用户管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;添加用户</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">用户添加</el-divider>
        <el-form
            :model="adminForm"
            :rules="rules"
            ref="adminForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="用户名称:" prop="username">
            <el-input v-model="adminForm.username" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="用户密码:" prop="password">
            <el-input v-model="adminForm.password" placeholder="请输入用户密码" type="password"></el-input>
          </el-form-item>
          <el-form-item label="用户昵称:" prop="nickname">
            <el-input v-model="adminForm.nickname" placeholder="请输入用户昵称"></el-input>
          </el-form-item>
          <el-form-item label="用户头像:" prop="avatar">
            <el-input v-model="adminForm.avatar" placeholder="请输入用户头像"></el-input>
          </el-form-item>
          <el-form-item label="用户手机:" prop="phone">
            <el-input v-model="adminForm.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱:" prop="email">
            <el-input v-model="adminForm.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item label="用户简介:" prop="description">
            <el-input v-model="adminForm.description" placeholder="请输入用户简介"></el-input>
          </el-form-item>

          <el-row>
            <el-col :span="6">
              <el-form-item label="是否启用" align="center" style="float: left">
                <el-switch
                    v-model="adminForm.enable"
                    :active-value="1"
                    :inactive-value="0"
                    active-color="#13ce66"
                    inactive-color="#999">
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否启用经理" align="center" style="float: left">
                <el-switch
                    v-model="adminForm.manager"
                    :active-value="1"
                    :inactive-value="0"
                    active-color="#13ce66"
                    inactive-color="#999">
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户部门:" prop="deptIds">
                <el-select v-model="adminForm.deptIds"   placeholder="请选择用户部门">
                  <el-option
                      v-for="item in adminListOptions"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-divider content-position="left">确认用户添加</el-divider>
          <el-form-item>
            <el-button type="primary" @click="submitForm('adminForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('adminForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      adminListOptions: [],
      adminForm: {
        username: '',
        nickname: '',
        password: '',
        avatar: '',
        phone: '',
        email: '',
        description: '',
        enable: 1,
        manager: 0,
        deptIds: []
      },
      rules: {
        username: [
          {required: true, message: "请输入用户名称", trigger: 'blur'},
          {pattern: /^[A-Za-z]+$/, message: "请输入英文", trigger: 'blur'}
        ],
        password: [
          {required: true, message: "请输入用户密码", trigger: 'blur'},
          {pattern: /^.{8,12}$/, message: "请输入长度8-12位的密码", trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: "请输入用户昵称", trigger: 'blur'},
          {pattern: /^[\u4e00-\u9fa5]{2,5}$/, message: "请输入中文", trigger: 'blur'}
        ],
        avatar: [
          {required: true, message: "请输入用户头像地址", trigger: 'blur'},
        ],
        phone: [
          {required: true, message: "请输入用户电话", trigger: 'blur'},
          {
            pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: "请输入正确的电话格式",
            trigger: 'blur'
          }
        ],
        email: [
          {required: true, message: "请输入用户邮箱", trigger: 'blur'},
          {pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: "请输入正确邮箱格式", trigger: 'blur'}
        ],
        description: [
          {required: true, message: "请输入用户简介", trigger: 'blur'},
          {
            pattern: /([\u4E00-\u9FA5])+([\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c])*/,
            message: "请输入中文",
            trigger: 'blur'
          }
        ],
        deptIds: [
          {required: true, message: "请选择部门", trigger: 'blur'},
        ],
        enable: [
          {required: true, message: "请选择用户启用", trigger: 'change'},
        ],
        manager: [
          {required: true, message: "请选择用户是否启用经理", trigger: 'change'},
        ]
      }
    }
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9090/admins/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.adminForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加用户成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadAdminNList() {
      let url = 'http://localhost:9090/depts';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.adminListOptions = responseBody.data;
      });
    }
  },
  mounted() {
    this.loadAdminNList();
  }
}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>